#ifndef _51A8B2F4_505C_459F_981C_1127BEACD3E7
#define _51A8B2F4_505C_459F_981C_1127BEACD3E7

#include "StrUtil.h"

namespace std
{
    class stringx: public string
    {
    public:
        using BaseClass = string;

#if 1 // 使用继承基类的构造函数替代冗余代码
        using string::string; 
#else
        stringx():string(){}

        stringx(const string& s): string(s)
        {
        }
#endif

        int compare_nocase(const char* s)
        {
            return x2lib::StrUtil::Compare(BaseClass::data(), s, true);
        }

        stringx get_lower()
        {
            stringx str = stringx(BaseClass::length(), 0);
            x2lib::StrUtil::ToLower((char*)str.data());
            return str;
        }

        stringx get_upper()
        {
            stringx str = stringx(BaseClass::length(), 0);
            x2lib::StrUtil::ToUpper((char*)str.data());
            return str;
        }

        stringx get_left(size_t count) const
        {
            return BaseClass::substr(0, count).data();
        }

        stringx get_mid(size_t pos, size_t count) const
        {
            return BaseClass::substr(pos, count).data();
        }

        stringx get_right(size_t count) const
        {
            int pos = BaseClass::length() - count;
            if (pos < 0)
            {
                pos = 0;
                count = BaseClass::length();
            }
            return BaseClass::substr(pos, count).data();
        }

        long to_int(int radix)
        {
            return strtol(BaseClass::data(), NULL, radix);
        }

        unsigned long to_uint(int radix)
        {
            return strtoul(BaseClass::data(), NULL, radix);
        }

        double to_float(int radix)
        {
            return strtof(BaseClass::data(), NULL);
        }

        void from_int(int64_t i, const char *fmt = nullptr)
        {
#ifdef __X2LIB_WIN32__
            format((fmt && fmt[0]) ? fmt : "%I64d", i);
#else
            format((fmt && fmt[0]) ? fmt : "%lld", i);
#endif
        }

        void from_float(double f, const char *fmt = nullptr)
        {
            format((fmt && fmt[0]) ? fmt : "%f", f);
        }

        bool replace(const char* from_s, const char* to_s)
        {
            size_t index = BaseClass::find(from_s);
            if (index == BaseClass::npos)
            {
                return false;
            }

            BaseClass::replace(index, strlen(from_s), to_s);
            return true;
        }

        void format(const char* format, ...)
        {
            va_list body;
            va_start(body, format);
            int nChars = vsnprintf(0, 0, format, body) + 1;
            this->resize(nChars);
            vsprintf((char*)this->c_str(), format, body);
            va_end(body);
        }

        void append_format(const char* fmt, ...)
        {
            char* pData = (char*)calloc(1, 1024);
            va_list body;
            va_start(body, fmt);
            int nData = vsnprintf(pData, 1024, fmt, body) + 1;
            if (nData > 1024)
            {
                pData = (char*)realloc(pData, nData);
                vsnprintf(pData, nData, (char*)fmt, body);
            }
            pData[nData] = 0;
            BaseClass::append(pData);
            free(pData);
        }
    };
}

#endif
